#region imported Namespaces
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;

using BO = PHSRAG.Insight21.BusinessObjects;
using Utility = PHSRAG.Utility;
using ICachePolicy = PHSRAG.Insight21.Policy.ICachePolicy;
using GenerateKey = PHSRAG.Insight21.BusinessObjects.ModuleKey;
#endregion

namespace PHSRAG.Insight21.Admin
{
	/// <summary>
	/// Summary description for User.
	/// </summary>
	public class ModuleRoles : BO.BusinessObject
	{
		#region Constants
		private const string ModuleRolesTableName = "ModuleRole";
		private static string ModuleRolesKey = GenerateKey.MakeKey("ModuleRole_BA93A5EA-1C8D-4474-93ED-BB9C92CC9DFB",GenerateKey.Admin);
		#endregion

		#region Instance Variables
		#endregion

		#region Public Properties

        /// <summary>
        /// Gets the DataTable that contains a user's module roles details.
        /// </summary>
        public DataTable Details
		{
			get 
			{
				return DataContainer.Tables[ModuleRolesTableName];
			}
		}

        /// <summary>
        /// Gets the DataView that contains only those module roles that are granted/revoked via the UI.
        /// </summary>
        public DataView DetailsForUI
        {
            get 
            {
                DataView dataView = new DataView(DataContainer.Tables[ModuleRolesTableName]);
                dataView.RowFilter = "IsGrantedViaUI = 1";
                return dataView;
            }
        }
        #endregion

		#region Constructor
		/// <summary>
		/// Initialize the state of this object and create commands to handle insert/delete/update.
		/// </summary>
		public ModuleRoles(DataSet dataSet, ICachePolicy cachePolicy) : base(cachePolicy)
		{
			DataContainer = dataSet;
		}

		/// <summary>
		/// Initialize the data containner.
		/// </summary>
		public ModuleRoles(ICachePolicy cachePolicy) : base(cachePolicy)
		{
			DataContainer = new DataSet();
		}
		#endregion

		#region Base Methods
		public override void Cache()
		{
			CachePolicy.Cache(ModuleRolesKey, this);
		}

		public override void UnCache()
		{
			CachePolicy.UnCache(ModuleRolesKey);
		}

		/// <summary>
		/// Loads the data container with data from repository.
		/// </summary>
		/// <param name="connectionString">The connection string to the data source.</param>
		/// <param name="args">arguments passed into the method.</param>
		public override void Load( string connectionString, params object[] args )
		{
			if ( args.Length == 0 )
			{
				throw new Exception ("ModuleRoles Load expects at least one argument." );
			}		

			try
			{	
				Utility.DataAccess.LoadDataSet(DataContainer, ModuleRolesTableName, 
					connectionString, "GetUserModuleRoles",
					Utility.DataAccess.BuildSqlParameterArray("@applicationUserId", SqlDbType.Int, (int)args[0]));
			}
			catch( Exception e)
			{
				Utility.ExceptionReport.WriteToEventLog ("ModuleRoles", e, true, Utility.ExceptionReport.ReportType.Error);
			}
		}

		#endregion

	}
}
